

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xyz.User;
import com.xyz.UserType;

import services.UsersService;

/**
 * Servlet implementation class LoginReaction
 */
@WebServlet("/LoginReaction")
public class LoginReaction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginReaction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
		String login=request.getParameter("login");
		User uFromDB=UsersService.getInstance().getUserByLogin(login);
		
		if (uFromDB!=null && uFromDB.getPass().equals(request.getParameter("pass"))){
			System.out.println("OK");
			HttpSession session=request.getSession();
			session.setAttribute("user", uFromDB);
			
			if (uFromDB.getUserType()==UserType.admin){
				RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/jsp/admin_page.jsp");
				view.forward(request, response);
				//response.sendRedirect("/WEB-INF/jsp/admin_page.jsp");
			}
			else if (uFromDB.getUserType()==UserType.callCenter){
				RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/jsp/call_centre_page.jsp");
				view.forward(request, response);
			}
			else{
				RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/jsp/manager_page.jsp");
				view.forward(request, response);
			}
		}
		
		else{
			//System.out.println("something is wrong");
			//System.out.println(uFromDB.getPass());
			//System.out.println(request.getParameter("pass"));
		}
		return;
	}

}
